package com.leyu.mall.admin.cms.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.leyu.mall.domain.cms.CmsADLink;
import com.leyu.mall.service.cms.ICmsADLinkService;

/**
 * 〈一句话功能简述〉 〈功能详细描述〉
 * 
 * @author OOKK
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Controller
@Scope(value = "prototype")
@RequestMapping("admin/adlink")
public class CmsADLinkController {
	
	
	@Resource(name = "cmsADLinkServiceImpl")
	private ICmsADLinkService cmsADLinkServiceImpl;

	@RequestMapping(value = "save", method = RequestMethod.POST)
	public String save(Model model, CmsADLink cmsADLink) {
		try {
			cmsADLinkServiceImpl.save(cmsADLink);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "redirect:../adlink";
	}

	@RequestMapping(value = "delete/{adlinkId}", method = RequestMethod.GET)
	public String delete(Model model, @PathVariable long adlinkId) {
		try {
			cmsADLinkServiceImpl.deleteCmsADLink(adlinkId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "redirect:../../adlink";
	}

	@RequestMapping(value = "edit/{adlinkId}", method = RequestMethod.GET)
	public String edit(Model model, @PathVariable long adlinkId) {
		try {
			model.addAttribute("entity", cmsADLinkServiceImpl
					.getCmsADLinkById(adlinkId));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "admin/editadlink.html";
	}

	@ExceptionHandler(Exception.class)
	public String exception(Exception e, HttpServletRequest request) {
		request.setAttribute("exception", e);
		return "/error.html";
	}

	@RequestMapping(value = "new", method = RequestMethod.GET)
	public String newADLink(Model model) throws Exception {
		return "admin/newadlink.html";
	}

	@RequestMapping(value = "", method = RequestMethod.GET)
	public String index(Model model) throws Exception {
		try {
		model.addAttribute("list", cmsADLinkServiceImpl.getAllCmsADLink());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "admin/adlink.html";
	}
}
